package com.budwk.app.sqfwV2.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.alibaba.excel.util.StringUtils;
import com.alibaba.fastjson.JSONObject;
import com.budwk.app.annotation.ApiIdempotent;
import com.budwk.app.exception.ServiceException;
import com.budwk.app.sqfw.enums.SqfwFormSummaryFieldEnum;
import com.budwk.app.sqfw.models.Sqfw_formSummary;
import com.budwk.app.sqfw.models.Sqfw_formSummary_new;
import com.budwk.app.sqfw.services.SqfwFormsummaryNewService;
import com.budwk.app.sqfw.vo.Sqfw_formSummaryForAdminVO;
import com.budwk.app.sqfwV2.dto.PlaceParamDTO;
import com.budwk.app.sqfwV2.dto.dto.SqfwShopBaseInfoDTO;
import com.budwk.app.sqfwV2.dto.entity.SqfwShopsBaseInfo;
import com.budwk.app.sqfwV2.service.ShopsBaseInfoService;
import com.budwk.app.sys.models.Sys_unit;
import com.budwk.starter.common.openapi.annotation.*;
import com.budwk.starter.common.result.Result;
import com.budwk.starter.log.annotation.SLog;
import com.budwk.starter.security.utils.SecurityUtil;
import lombok.extern.slf4j.Slf4j;
//import org.eclipse.jetty.util.ajax.JSON;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.*;
import org.nutz.mvc.upload.TempFile;
import org.nutz.mvc.upload.UploadAdaptor;
import org.apache.commons.lang3.ObjectUtils;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author zhangp
 * @description TODO
 * @className ShopBaseInfoController
 * @Date 11:29 2024/3/1
 */
@IocBean
@At("/platform/sqfwV2/shopBaseInfo")
@SLog(tag = "商铺基础信息")
@ApiDefinition(tag = "商铺基础信息")
@Slf4j
public class ShopBaseInfoController {

    @Inject
    private ShopsBaseInfoService shopsBaseInfoService;
    @Inject
    private SqfwFormsummaryNewService summaryNewService;

    /**
     * 后台管理系统商铺列表查询（用于后台管理系统）
     */
    @At("/list")
    @Ok("json")
    @POST
    @ApiOperation(name = "后台管理系统商铺列表查询")
    @ApiFormParams(
            value = {
                    @ApiFormParam(name = "pageNo", example = "1", description = "页码", type = "integer"),
                    @ApiFormParam(name = "pageSize", example = "10", description = "页大小", type = "integer"),
                    @ApiFormParam(name = "pageOrderName", example = "createdAt", description = "排序字段"),
                    @ApiFormParam(name = "pageOrderBy", example = "descending", description = "排序方式")
            },
            implementation = Sqfw_formSummaryForAdminVO.class
    )
    @ApiResponses(
            implementation = Sqfw_formSummary.class
    )
    @SLog(tag = "后台管理系统任务列表查询", value = "${args[0]}")
    @SaCheckLogin
    public Result<?> list(@Param("..") SqfwShopBaseInfoDTO bean, @Param("pageNo") int pageNo, @Param("pageSize") int pageSize, @Param("pageOrderName") String pageOrderName, @Param("pageOrderBy") String pageOrderBy) {
        return shopsBaseInfoService.getShopInfoList(bean, pageNo, pageSize, pageOrderName, pageOrderBy);
    }

    @AdaptBy(type = UploadAdaptor.class, args = {"ioc:fileUpload"})
    @At()
    @Ok("json")
    @POST
    @ApiOperation(name = "导入商铺信息")
    @ApiFormParams(
            value = {@ApiFormParam(name = "file", description = "文件对象名")},
            mediaType = "multipart/form-data"
    )
    @SaCheckLogin
    public Result<?> importShopInfoData(@Param("file") TempFile file, @Param("communityId") String communityId) {
        return shopsBaseInfoService.importShopInfoData(file, communityId);
    }

    /**
     * 后台管理系统查询社区列表查询（用于后台管理系统）
     */
    @At("/queryCommunity")
    @Ok("json")
    @POST
    @ApiOperation(name = "后台管理系统查询社区列表查询")
    @ApiFormParams(
            implementation = Sys_unit.class
    )
    @ApiResponses(
            implementation = Sys_unit.class
    )
    @SLog(tag = "后台管理系统查询社区列表查询", value = "${args[0]}")
    @SaCheckLogin
    public Result<?> queryCommunity(@Param("communityName") String communityName) {
        return shopsBaseInfoService.queryCommunity(communityName);
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @At("/batchDelete")
    @Ok("json")
    @POST
    @ApiOperation(name = "批量删除表数据")
    @SLog(tag = "批量删除表数据", value = "${args[0]}")
    @SaCheckLogin
    public Result<?> delete(@Param("ids") List<String> ids) {
        shopsBaseInfoService.delete(ids);
        return Result.success();
    }

    @At("/update")
    @Ok("json")
    @POST
    @SLog(tag = "修改商铺信息", value = "${args[0]}")
    @ApiOperation(name = "修改商铺信息")
    @ApiFormParams(
            implementation = SqfwShopsBaseInfo.class
    )
    @ApiResponses
    @SaCheckLogin
    @ApiIdempotent
    public Result<?> update(@Param("..") SqfwShopsBaseInfo info) {
        log.info("ShopBaseInfoController.update 请求参数：SqfwShopsBaseInfo：{}", JSONObject.toJSONString(info));
        return Result.data(this.shopsBaseInfoService.updateShopInfo(info));
    }

    @At("/updateBusinessStatus")
    @Ok("json")
    @POST
    @SLog(tag = "启用/关闭商铺状态", value = "${args[0]}")
    @ApiOperation(name = "启用/关闭")
    @ApiFormParams(
            implementation = SqfwShopsBaseInfo.class
    )
    @ApiResponses
    @SaCheckLogin
    public Result<?> updateBusinessStatus(@Param("..") SqfwShopsBaseInfo info) {
        SqfwShopsBaseInfo shopsBaseInfo = shopsBaseInfoService.fetch(info.getId());
        if (ObjectUtils.isEmpty(shopsBaseInfo)) {
            return Result.error("商铺信息不存在！");
        }
        shopsBaseInfo.setUpdateId(SecurityUtil.getUserId());
        shopsBaseInfo.setUpdateDate(LocalDateTime.now());
        shopsBaseInfo.setDeleted(info.getDeleted());
        shopsBaseInfo.setShopStatus(info.getDeleted() ?SqfwFormSummaryFieldEnum.CHECK_STATUS_CLOSED.getCode():SqfwFormSummaryFieldEnum.check_status_init.getCode());
        shopsBaseInfoService.updateIgnoreNull(shopsBaseInfo);
        return Result.success();
    }

    @At("/downloadTemplate")
    @Ok("json")
    @GET
    @SLog(tag = "模板下载", value = "")
    @ApiOperation(name = "模板下载")
    @ApiResponses
    @SaCheckLogin
    public Result<?> downloadTemplate() {
        return Result.data(shopsBaseInfoService.downloadTemplate());
    }



    /**
     * 根据当前登录人获取场所
     *
     * @return
     */
    @At("/getPlaceInfoList")
    @Ok("json")
    @POST
    @ApiOperation(name = "根据当前登录人获取场所")
    @ApiResponses
    @SLog(tag = "根据当前登录人获取场所", value = "")
    @SaCheckLogin
    public Result<?> getPlaceInfoList(@Param("..") PlaceParamDTO placeParamDTO) {
        log.info("ShopBaseInfoController.getPlaceInfoList 请求参数：placeParamDTO：{}", JSONObject.toJSONString(placeParamDTO));
        return shopsBaseInfoService.getPlaceInfoList(placeParamDTO);
    }

    @At("/save")
    @Ok("json")
    @POST
    @SLog(tag = "新增商铺信息", value = "${args[0]}")
    @ApiOperation(name = "新增商铺信息")
    @ApiFormParams(
            implementation = SqfwShopsBaseInfo.class
    )
    @ApiResponses
    @SaCheckLogin
    @ApiIdempotent
    public Result<?> save(@Param("..") SqfwShopsBaseInfo info) {
        log.info("ShopBaseInfoController.save 请求参数：SqfwShopsBaseInfo：{}", JSONObject.toJSONString(info));
        return Result.data(shopsBaseInfoService.saveShopInfo(info));
    }

    @At("/getShopInfoDetail")
    @Ok("json")
    @GET
    @SLog(tag = "获取详情", value = "")
    @ApiOperation(name = "获取场所详情")
    @ApiResponses
    @SaCheckLogin
    public Result<?> getShopInfoDetail(@Param("id") String id) {
        return Result.data(shopsBaseInfoService.getShopInfoDetail(id));
    }

}
